home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / nntp.c < prev    next >
C/C++ Source or Header  |  1996-06-28  |  6KB  |  155 lines

  1. /*
  2.  * Program:    Network News Transfer Protocol (NNTP) routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    10 February 1992
  13.  * Last Edited:    28 June 1996
  14.  *
  15.  * Copyright 1996 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notices appear in all copies and that both the
  20.  * above copyright notices and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  30.  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN
  32.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. #include <ctype.h>
  38. #include <stdio.h>
  39. #include "mail.h"
  40. #include "osdep.h"
  41. #include "smtp.h"
  42. #include "nntp.h"
  43. #include "rfc822.h"
  44. #include "misc.h"
  45.  
  46.  
  47. /* Mailer parameters */
  48.  
  49. long nntp_port = 0;        /* default port override */
  50.  
  51. /* Network News Transfer Protocol open connection
  52.  * Accepts: service host list
  53.  *        initial debugging flag
  54.  * Returns: T on success, NIL on failure
  55.  */
  56.  
  57. SMTPSTREAM *nntp_open (char **hostlist,long debug)
  58. {
  59.   SMTPSTREAM *stream = NIL;
  60.   long i;
  61.   void *tcpstream;
  62.   if (!(hostlist && *hostlist)) mm_log ("Missing NNTP service host",ERROR);
  63.   else do {            /* try to open connection */
  64.     if (tcpstream = nntp_port ? tcp_open (*hostlist,NIL,nntp_port) :
  65.     tcp_open (*hostlist,"nntp",NNTPTCPPORT)) {
  66.       stream = (SMTPSTREAM *) fs_get (sizeof (SMTPSTREAM));
  67.       stream->tcpstream = tcpstream;
  68.       stream->debug = (debug & OP_DEBUG) ? T : NIL;
  69.       stream->reply = NIL;
  70.       i = smtp_reply (stream);    /* get server greeting */
  71.       if (debug & OP_READONLY) {/* if just want to read, either code is OK */
  72.     if ((i == NNTPGREET) || (i == NNTPGREETNOPOST))
  73.       mm_log (stream->reply + 4,(long) NIL);
  74.     else {            /* oops */
  75.       mm_log (stream->reply,ERROR);
  76.       stream = smtp_close (stream);
  77.     }
  78.       }
  79.       else if (i != NNTPGREET) {/* want to post */
  80.     mm_log (stream->reply,ERROR);
  81.     stream = smtp_close (stream);
  82.       }
  83.     }
  84.   } while (!stream && *++hostlist);
  85.                 /* some silly servers require this */
  86.   if (stream) nntp_send (stream,"MODE","READER");
  87.   return stream;
  88. }
  89.  
  90. /* Network News Transfer Protocol deliver news
  91.  * Accepts: stream
  92.  *        message envelope
  93.  *        message body
  94.  * Returns: T on success, NIL on failure
  95.  */
  96.  
  97. long nntp_mail (SMTPSTREAM *stream,ENVELOPE *env,BODY *body)
  98. {
  99.   long ret = NIL;
  100.   char tmp[8*MAILTMPLEN],*s;
  101.                 /* negotiate post command */
  102.   if (nntp_send (stream,"POST",NIL) == NNTPREADY) {
  103.                 /* set up error in case failure */
  104.     smtp_fake (stream,SMTPSOFTFATAL,"NNTP connection went away!");
  105.     /* Gabba gabba hey, we need some brain damage to send netnews!!!
  106.      *
  107.      * First, we give ourselves a frontal lobotomy, and put in some UUCP
  108.      *  syntax.  It doesn't matter that it's completely bogus UUCP, and
  109.      *  that UUCP has nothing to do with anything we're doing.
  110.      *
  111.      * Then, we bop ourselves on the head with a ball-peen hammer.  How
  112.      *  dare we be so presumptious as to insert a *comment* in a Date:
  113.      *  header line.  Why, we were actually trying to be nice to a human
  114.      *  by giving a symbolic timezone (such as PST) in addition to a
  115.      *  numeric timezone (such as -0800).  But the gods of news transport
  116.      *  will have none of this.  Unix weenies, tried and true, rule!!!
  117.      */
  118.                 /* RFC-1036 requires this cretinism */
  119.     sprintf (tmp,"Path: %s!%s\015\012",tcp_localhost (stream->tcpstream),
  120.          env->from ? env->from->mailbox : "foo");
  121.                 /* here's another cretinism */
  122.     if (s = strstr (env->date," (")) *s = NIL;
  123.                 /* output data, return success status */
  124.     ret = tcp_soutr (stream->tcpstream,tmp) &&
  125.       (*(rfc822emit_t) mail_parameters (NIL,GET_RFC822OUTPUT,NIL))
  126.     (tmp,env,body,smtp_soutr,stream->tcpstream) &&
  127.       (nntp_send (stream,".",NIL) == NNTPOK);
  128.     if (s) *s = ' ';        /* put the comment in the date back */
  129.   }
  130.   return ret;
  131. }
  132.  
  133. /* NNTP send command
  134.  * Accepts: SEND stream
  135.  *        text
  136.  * Returns: reply code
  137.  */
  138.  
  139. long nntp_send (SMTPSTREAM *stream,char *command,char *args)
  140. {
  141.   char tmp[MAILTMPLEN],usr[MAILTMPLEN],pwd[MAILTMPLEN];
  142.   long ret = smtp_send (stream,command,args);
  143.   if ((ret == NNTPWANTAUTH) || (ret == NNTPWANTAUTH2)) {
  144.     sprintf (tmp,"{%s/nntp}",tcp_host (stream->tcpstream));
  145.                 /* get user name and password */
  146.     mm_login (tmp,usr,pwd,(long) 1);
  147.                 /* try it if user gave one */
  148.     if (*pwd && (nntp_send (stream,"AUTHINFO USER",usr) == NNTPWANTPASS) &&
  149.     (nntp_send (stream,"AUTHINFO PASS",pwd) == NNTPAUTHED))
  150.                 /* resend the command */
  151.       ret = nntp_send (stream,command,args);
  152.   }
  153.   return ret;
  154. }
  155.